package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationInserDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.AppointmentsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {
    @Autowired
    private MemberReservationMapper reservationMapper;
    @Override
    public int getCancelledCount() {
        //查询当前线程中的用户的取消预约的次数，并返回次数
        //select * from reservation where id = ? and status = 2 and update_time between ? and ?
        //首先获取当前线程中的用户id，当天的开始时间和结束时间
        Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<Reservation> list = lambdaQuery()
                .eq(Reservation::getUpdateBy, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getUpdateTime, startTime, endTime)
                .list();
        return list.size();
    }

    @Override
    public List<AppointmentsVo> getCountByTime(LocalDate date) {
        //-- 查询每个时间段剩余预约次数
        //select time, 6 - count(*) as count from reservation
        //    where time between #{start} and #{end}
        //    and status != 2 group by time;
        LocalDateTime startOfDay = date.atStartOfDay();
        LocalDateTime endOfDay = date.atTime(23, 59, 59);

        List<AppointmentsVo> list =reservationMapper.getCountByTime(startOfDay,endOfDay);


        return null;
    }

    @Override
    public void addReservation(ReservationInserDto dto) {
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        reservation.setStatus(0);
        save(reservation);
    }
}
